--安装包
--https://gitee.com/disfigure/Skye/raw/master/Download/eqswfgth.zip
--图片
--https://gitee.com/static/images/ma_icon.svg

local request = {}

-- @name 网络请求
-- @param Network_address 地址
-- @param parameter 参数
-- @return bool布尔值
-- @author wh
function request:Network(Network_address, parameter)
    local response = gg.makeRequest(Network_address, nil, parameter)
    if not response or not response.content then
        gg.alert(string.format("网络错误，请检查你的网络\n状态码：%s", 
            response and response.code or "未知"))
        return false
    end
    return response.content
end

-- @name 写入文件
-- @param Path 路径
-- @param Type 类型
-- @param Content 内容
-- @return bool布尔值
-- @author wh
function Write_file(Path, Type, Content)
    local f, err = io.open(Path, Type)
    if not f then
        gg.alert("无法创建/写入文件："..Path.."\n错误："..tostring(err))
        return false
    end
    f:write(Content)
    f:close()
    return true
end

-- @name 下载文件
-- @param address 地址
-- @param con 内容
-- @return bool布尔值
-- @author wh
-- @time 2025-8-21
function request:download(address,con)
    local content = self:Network(address)
    if content == false then
        return false
    end
    return Write_file(address, "w", con)
end

local a = request:Network("https://gitee.com/disfigure/Skye/raw/master/Skye_Config_Version.lua")
if a == false then
    gg.alert("网络错误，请检查你的网络")
    return
end
local b = load(a)()
if b['Skye_Config_Version']['Version'] == nil then
    gg.alert("网络错误，请检查你的网络")
    return
end

-- 设置路径
local Path = "/storage/emulated/0/Cloud-Xm/"
local zip_path = Path .. b['Skye_Config_Version']['Congfig_Zip']

if not file.exists(Path) then
    file.mkdir(Path)
else
    -- 检查文件是否存在
    if not file.exists(zip_path) then
        gg.toast("开始下载配置包...")
        local download_url = "https://gitee.com/disfigure/Skye/raw/master/Download/"..b['Skye_Config_Version']['Congfig_Zip']
        local content = request:Network(download_url)
        if content == false then
            gg.alert("下载失败！")
            return false
        end
        local success = Write_file(zip_path, "w", content)
        if not success then
            gg.alert("下载失败！")
            return
        end
    else
        gg.toast("使用本地缓存配置包")
    end
end

  
--  print(file.zip(zip_path,Path,"","unzip"))
-- 解压ZIP文件
local ok, err = file.zip(zip_path,Path,"","unzip")
if not ok then
    gg.alert("解压失败: "..err)
    return
end

gg.toast("配置更新完成！版本号: "..b['Skye_Config_Version']["Version"])